package com.asdf.application.commands;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.asdf.application.commands.dialogHandlers.DialogHandlerCancel;
import com.asdf.application.commands.dialogHandlers.DialogHandlerConnect;
import com.asdf.application.commands.dialogHandlers.DialogHandlerNewUser;
import com.asdf.application.commands.dialogHandlers.IDialogHandler;
import com.asdf.external.INetserviceSimulator;
import com.asdf.ui.ConnectDialog;

/**
 * @author Artem Barger
 * 
 * Class which capable to handle user connection process, after system 
 * receives appropriate command from command line.
 *
 */
public class ConnectCommand implements ICommandLine, ActionListener
{
	private static final String	CONNECT_DLG_CANCEL		= "CONNECT_DLG_CANCEL";
	private static final String	CONNECT_DLG_CONNECT		= "CONNECT_DLG_CONNECT";
	private static final String	CONNECT_DLG_NEWUSER		= "CONNECT_DLG_NEWUSER";
	
	private Map<String, IDialogHandler>	handlers		= new HashMap<String, IDialogHandler>( );
	private INetserviceSimulator		simulator;
	
	@Override
	public void execute( INetserviceSimulator simulator, List<String> parameters)
	{
		ConnectDialog 	dlgConnect = new ConnectDialog( );

		this.simulator = simulator;
		
		handlers.put( CONNECT_DLG_CANCEL, new DialogHandlerCancel( dlgConnect));
		dlgConnect.registerEventHandlerCancelCommand( CONNECT_DLG_CANCEL, this);
		
		handlers.put( CONNECT_DLG_CONNECT, new DialogHandlerConnect( dlgConnect));
		dlgConnect.registerEventHandlerConnectCommand( CONNECT_DLG_CONNECT, this);
		
		handlers.put( CONNECT_DLG_NEWUSER, new DialogHandlerNewUser( dlgConnect));
		dlgConnect.registerEventHandlerNewUserCommand( CONNECT_DLG_NEWUSER, this);
		
		dlgConnect.setVisible( true);
	}

	@Override
	public void actionPerformed( ActionEvent e)
	{
		if ( handlers.containsKey( e.getActionCommand( )))
			handlers.get( e.getActionCommand( )).handle( simulator);
	}

}
